package com.hujing.io.util;

import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.xwpf.usermodel.ParagraphAlignment;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;

import java.io.FileOutputStream;
import java.io.IOException;

public class PoiUtil {

    public static void main(String[] args) {
        // 生成excel并写入数据
//        generateExcel();
        // 生成word并写入数据
//        generateWord();
        // 写入65536条数据到xls文件中
//        generate03Excel();
//        generate07Excel(); //18秒
        generate07ExcelPlus();//4秒
    }
    
    public static void generate07ExcelPlus() {
        long begin = System.currentTimeMillis();
        Workbook workbook = new SXSSFWorkbook();
        Sheet sheet = workbook.createSheet();
        for (int i = 0; i < 500000; i++) {
            Row row = sheet.createRow(i);
            for (int j = 0; j < 10; j++) {
                row.createCell(j).setCellValue(j);
            }
        }
        try (FileOutputStream fos = new FileOutputStream("07.xlsx")) {
            workbook.write(fos);
        } catch (IOException e) {
            e.printStackTrace();
        }
        long end = System.currentTimeMillis();
        System.out.println("文件写入完成，耗时：" + (end - begin) / 1000 + "秒");
        ((SXSSFWorkbook)workbook).dispose();
    }

    private static void generate07Excel() {
        long begin = System.currentTimeMillis();
        Workbook workbook = new XSSFWorkbook();
        Sheet sheet = workbook.createSheet();
        for (int i = 0; i < 500000; i++) {
            Row row = sheet.createRow(i);
            for (int j = 0; j < 10; j++) {
                row.createCell(j).setCellValue(j);
            }
        }
        try (FileOutputStream fos = new FileOutputStream("07.xlsx")) {
            workbook.write(fos);
        } catch (IOException e) {
            e.printStackTrace();
        }
        long end = System.currentTimeMillis();
        System.out.println("文件写入完成，耗时：" + (end - begin) / 1000 + "秒");
    }

    private static void generate03Excel() {
        long begin = System.currentTimeMillis();
        Workbook workbook = new HSSFWorkbook();
        Sheet sheet = workbook.createSheet();
        for (int i = 0; i < 65536; i++) {
            Row row = sheet.createRow(i);
            for (int j = 0; j < 10; j++) {
                row.createCell(j).setCellValue(j);
            }
        }
        try (FileOutputStream fos = new FileOutputStream("03.xls")) {
            workbook.write(fos);
        } catch (IOException e) {
            e.printStackTrace();
        }
        long end = System.currentTimeMillis();
        System.out.println("文件写入完成，耗时：" + (end - begin) / 1000 + "秒");
    }

    private static void generateWord() {
        XWPFDocument xwpfDocument = new XWPFDocument();
        XWPFParagraph paragraph = xwpfDocument.createParagraph();
        // 设置段落居中
        paragraph.setAlignment(ParagraphAlignment.CENTER);


        XWPFRun run = paragraph.createRun();
        run.setText("这是一段文字");

        try (FileOutputStream fos = new FileOutputStream("test.docx")) {
            xwpfDocument.write(fos);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static void generateExcel() {
        // 创建工作薄
        XSSFWorkbook workbook = new XSSFWorkbook();
        // 创建工作表
        XSSFSheet sheet = workbook.createSheet("员工信息");
        // 创建行
        XSSFRow row = sheet.createRow(0);
        // 创建单元格并写入数据
        row.createCell(0).setCellValue("姓名");
        row.createCell(1).setCellValue("年龄");

        // 写入第二行数据
        XSSFRow row1 = sheet.createRow(1);
        row1.createCell(0).setCellValue("张三");
        row1.createCell(1).setCellValue(25);

        // 输出到文件
        try (FileOutputStream fos = new FileOutputStream("员工信息.xlsx")) {
            workbook.write(fos);
            System.out.println("写入成功");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                workbook.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }


}
